package org.forever.view
{
	import flash.geom.Point;
	
	import spark.components.BorderContainer;

	/**折线的箭头类*/
	public class BrokenTriangle extends BorderContainer
	{
		//箭头所需参数,等边三角形
		private var _fromRect:BrokenLineRect;//起始矩形
		private var _toRect:BrokenLineRect;//结束矩形
		private var _color:uint = 0x000000;//箭头颜色
		private var _len:Number = 10;//箭头的长度
		private var _fromPoint:Point = new Point();
		private var _toPoint:Point = new Point();
		
		public function BrokenTriangle()
		{
			setStyle("borderColor","#FFFFFF");
		}
		
		public function updateToAndFromPoint():void{
			_fromPoint.x = _fromRect.x + _fromRect.width/2;
			_fromPoint.y = _fromRect.y + _fromRect.height/2;
			_toPoint.x = _toRect.x + _toRect.width/2;
			_toPoint.y = _toRect.y + _toRect.height/2;
		}
		
		public function draw():void{
			updateToAndFromPoint();
			var vDistance:Number= toPoint.y-fromPoint.y;//起始点垂直距离
			var sDistance:Number=Point.distance(fromPoint,toPoint);//起始点直线距离
			var sinAValue:Number=vDistance/sDistance;//起始点间的夹角的sin值
			var radianA:Number=Math.asin(sinAValue);//两点间直线与水平线的角度(弧度)
			/**用于计算三角形顶点与目标点垂直距离的夹角(弧度)*/
			var vRadian:Number=radianA+Math.PI/6;
			/**上顶点与目标点的垂直距离*/
			var topYDis:Number=_len*Math.sin(vRadian);
			/**上顶点与目标点的水平距离*/
			var topXDis:Number=_len*Math.cos(vRadian);
			
			/**用于计算三角形顶点与目标点水平距离的夹角(弧度)*/
			var hRadian:Number=radianA-Math.PI/6;
			/**下顶点与目标点的垂直距离*/
			var botYDis:Number=_len*Math.sin(hRadian);
			/**下顶点与目标点的水平距离*/
			var botXDis:Number=_len*Math.cos(hRadian);
			
			/**计算三角形上下顶点坐标*/
			var topPointX:Number=toPoint.x-topXDis;
			var topPointY:Number=toPoint.y-topYDis;
			
			var botPointX:Number=toPoint.x-botXDis;
			var botPointY:Number=toPoint.y-botYDis;
			if(toPoint.x<fromPoint.x)
			{
				topPointX=toPoint.x+topXDis;
				botPointX=toPoint.x+botXDis;
			}
			
			graphics.clear();
			graphics.beginFill(_color);
			graphics.moveTo(topPointX,topPointY);
			graphics.lineTo(toPoint.x,toPoint.y);
			graphics.lineTo(botPointX,botPointY);
			graphics.lineTo(topPointX,topPointY);
		}

		public function get toPoint():Point
		{
			return _toPoint;
		}

		public function set toPoint(value:Point):void
		{
			_toPoint = value;
		}

		public function get fromPoint():Point
		{
			return _fromPoint;
		}

		public function set fromPoint(value:Point):void
		{
			_fromPoint = value;
		}

		public function get fromRect():BrokenLineRect
		{
			return _fromRect;
		}

		public function set fromRect(value:BrokenLineRect):void
		{
			_fromRect = value;
		}

		public function get toRect():BrokenLineRect
		{
			return _toRect;
		}

		public function set toRect(value:BrokenLineRect):void
		{
			_toRect = value;
		}

		
	}
}